package com.gz.spring.tx;

import com.gz.spring.tx.dao.GoodsDao;
import com.gz.spring.tx.service.GoodsService;
import com.gz.spring.tx.service.MultiplyService;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TxTest {

    @Test
    public void queryPriceByIdTest(){
        ApplicationContext ioc = new ClassPathXmlApplicationContext("tx_ioc.xml");
        GoodsDao goodsDao = ioc.getBean(GoodsDao.class);
        Float price = goodsDao.queryPriceById(1);
        System.out.println(price);
    }

    @Test
    public void updateBalance(){
        ApplicationContext ioc = new ClassPathXmlApplicationContext("tx_ioc.xml");
        GoodsDao goodsDao = ioc.getBean(GoodsDao.class);
        goodsDao.updateBalance(1,1.0f);
        System.out.println("减少用户余额成功");
    }

    @Test
    public void updateAmount(){
        ApplicationContext ioc = new ClassPathXmlApplicationContext("tx_ioc.xml");
        GoodsDao goodsDao = ioc.getBean(GoodsDao.class);
        goodsDao.updateAmount(1,2);
        System.out.println("减少库存成功");
    }

    @Test
    public void buyGoods(){
        ApplicationContext ioc = new ClassPathXmlApplicationContext("tx_ioc.xml");
        GoodsService goodsService = ioc.getBean(GoodsService.class);
        goodsService.buyGoods(1,1,1);
        System.out.println("购买成功");
    }

    @Test
    public void buyGoodsByTx(){ //带有事务处理
        ApplicationContext ioc = new ClassPathXmlApplicationContext("tx_ioc.xml");
        GoodsService goodsService = ioc.getBean(GoodsService.class);
        goodsService.buyGoodsByTx(1,1,1);
        System.out.println("购买成功");
    }

    @Test
    public void buyGoodsByMultiplyTx(){ //带有事务处理
        ApplicationContext ioc = new ClassPathXmlApplicationContext("tx_ioc.xml");
        MultiplyService multiplyService = ioc.getBean(MultiplyService.class);
        multiplyService.multiplyBuyGoodsByTx();
        System.out.println("购买成功");
    }
}
